document.addEventListener('DOMContentLoaded', function() {
    // 验证用户登录状态
    const users = JSON.parse(localStorage.getItem('registeredUsers')) || [];
    const loginEmail = localStorage.getItem('currentLoginEmail');
    
    if (!loginEmail) {
        window.location.href = 'index.html';
        return;
    }
    
    // 获取当前用户信息
    const currentUser = users.find(user => user.email === loginEmail);
    if (currentUser) {
        // 填充用户信息
        document.getElementById('user-display-name').textContent = currentUser.name;
        
        // 更新头像
        if (currentUser.avatar) {
            document.getElementById('header-avatar').src = currentUser.avatar;
        }
    } else {
        localStorage.removeItem('currentLoginEmail');
        window.location.href = 'index.html';
    }
    
    // 移动端菜单切换
    const mobileMenuButton = document.getElementById('mobile-menu-button');
    const mobileMenu = document.getElementById('mobile-menu');
    
    mobileMenuButton.addEventListener('click', function() {
        mobileMenu.classList.toggle('hidden');
    });
    
    // 通知设置保存
    document.getElementById('save-notifications').addEventListener('click', function() {
        // 收集通知设置
        const notificationSettings = {
            inApp: document.getElementById('in-app-notifications').checked,
            email: document.getElementById('email-notifications').checked,
            sms: document.getElementById('sms-notifications').checked,
            task: document.getElementById('task-notifications').checked,
            message: document.getElementById('message-notifications').checked,
            system: document.getElementById('system-notifications').checked,
            marketing: document.getElementById('marketing-notifications').checked
        };
        
        // 保存到本地存储
        localStorage.setItem('notificationSettings', JSON.stringify(notificationSettings));
        showToast('通知设置已保存');
    });
    
    // 通知设置重置
    document.getElementById('reset-notifications').addEventListener('click', function() {
        // 重置为默认设置
        document.getElementById('in-app-notifications').checked = true;
        document.getElementById('email-notifications').checked = true;
        document.getElementById('sms-notifications').checked = false;
        document.getElementById('task-notifications').checked = true;
        document.getElementById('message-notifications').checked = true;
        document.getElementById('system-notifications').checked = true;
        document.getElementById('marketing-notifications').checked = false;
    });
    
    // 退出登录
    document.getElementById('logout-btn').addEventListener('click', function() {
        if (confirm('确定要退出登录吗？')) {
            localStorage.removeItem('currentLoginEmail');
            window.location.href = 'index.html';
        }
    });
    
    // 显示提示消息
    function showToast(message) {
        const toast = document.getElementById('success-toast');
        document.getElementById('toast-message').textContent = message;
        
        // 显示toast
        toast.classList.remove('translate-y-20', 'opacity-0');
        toast.classList.add('translate-y-0', 'opacity-100');
        
        // 3秒后隐藏
        setTimeout(() => {
            toast.classList.remove('translate-y-0', 'opacity-100');
            toast.classList.add('translate-y-20', 'opacity-0');
        }, 3000);
    }
    
    // 加载保存的通知设置
    function loadNotificationSettings() {
        const savedSettings = JSON.parse(localStorage.getItem('notificationSettings'));
        if (savedSettings) {
            document.getElementById('in-app-notifications').checked = savedSettings.inApp;
            document.getElementById('email-notifications').checked = savedSettings.email;
            document.getElementById('sms-notifications').checked = savedSettings.sms;
            document.getElementById('task-notifications').checked = savedSettings.task;
            document.getElementById('message-notifications').checked = savedSettings.message;
            document.getElementById('system-notifications').checked = savedSettings.system;
            document.getElementById('marketing-notifications').checked = savedSettings.marketing;
        } else {
            // 设置默认值
            document.getElementById('in-app-notifications').checked = true;
            document.getElementById('email-notifications').checked = true;
            document.getElementById('task-notifications').checked = true;
            document.getElementById('message-notifications').checked = true;
            document.getElementById('system-notifications').checked = true;
        }
    }
    
    // 页面加载时执行
    loadNotificationSettings();
    // 用户菜单下拉功能
            const userMenuButton = document.getElementById('user-menu-button');
            const userMenuDropdown = document.getElementById('user-menu-dropdown');
            
            // 点击头像切换菜单显示/隐藏
            userMenuButton.addEventListener('click', function(event) {
                event.stopPropagation(); // 防止事件冒泡
                userMenuDropdown.classList.toggle('hidden');
            });
            
            // 点击页面其他地方关闭菜单
            document.addEventListener('click', function(event) {
                if (!userMenuButton.contains(event.target) && !userMenuDropdown.contains(event.target)) {
                    userMenuDropdown.classList.add('hidden');
                }
            });
            
            // 退出登录功能
            document.getElementById('logout-button').addEventListener('click', function() {
                localStorage.removeItem('currentLoginEmail');
                window.location.href = 'index.html';
            });
            
     updateAvatar();
    
    // 监听头像更新事件
    window.addEventListener('avatarUpdated', function(e) {
        const headerAvatar = document.getElementById('header-avatar');
        if (headerAvatar) {
            headerAvatar.src = e.detail.avatar;
        }
    });
    
    // 监听本地存储变化（应对多标签页情况）
    window.addEventListener('storage', function(e) {
        if (e.key === 'registeredUsers') {
            updateAvatar();
        }
    });
});
